package com.nativetalk.controller.app.four.live;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.Ip;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.four.live.FourLiveService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by wangzheng on 2017/2/27.
 */
@Controller
public class FourLiveController {
    private static Logger logger = Logger.getLogger(FourLiveController.class);
    @Resource
    private FourLiveService fourLiveService;

    /**
     * @return RetInfo
     * @Purpose 设置学生进入直播课的状态
     * @version 4.1
     * @time 2017-02-27
     * @author wangzheng
     */
    @RequestMapping(value = "/app/11/live/member/in", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo memberIn(@RequestBody Map<String, Integer> mapParam,
                            @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(mapParam));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            int live_cou_his_id = mapParam.get("live_cou_his_id");
            RetInfo retInfo = fourLiveService.memberLoginIn(tdNtMember, live_cou_his_id);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 设置学生离开直播课的状态
     * @version 4.1
     * @time 2017-02-27
     * @author wangzheng
     */
    @RequestMapping(value = "/app/11/live/member/out", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo memberOut(@RequestBody Map<String, Integer> mapParam,
                             @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(mapParam));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            int live_cou_his_id = mapParam.get("live_cou_his_id");
            RetInfo retInfo = fourLiveService.memberLoginOut(tdNtMember, live_cou_his_id);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 获取直播课学生的队列信息
     * @version 4.1
     * @time 2017-02-27
     * @author wangzheng
     */
    @RequestMapping(value = "/app/11/live/member/queueInfo", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo memberQueueInfo(@RequestBody Map<String, Integer> mapParam) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(mapParam));

            int live_cou_his_id = mapParam.get("live_cou_his_id");
            RetInfo retInfo = fourLiveService.findMemberQueueInfo(live_cou_his_id);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 获取直播类别
     * @version 4.2
     * @time 2017-04-27
     * @author wangzheng
     */
    @RequestMapping(value = "/app/11/live/his/type", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo findLiveType() {
        try {
            //打印入参
            logger.info(Constants.prefix_in);
            RetInfo retInfo = fourLiveService.findLiveType();
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 直播打赏时跳转到第三方界面
     * @version 4.2
     * @time 2017-04-27
     * @author wangzheng on 2017-03-18
     */
    @RequestMapping(value = "/app/11/token/live/member/transfer", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo liveTransfer(HttpServletRequest request, @RequestBody Map<String, String> map,
                                @RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            RetInfo retInfo = new RetInfo();

            //参数校验
            String amount_str = map.get("amount");
            if (amount_str == null || "".equals(amount_str.trim())) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("参数错误：amount不能为空，amount=" + amount_str);
                logger.error("参数错误：amount不能为空，amount=" + amount_str);
                return retInfo;
            }
            Float amount = null;
            try {
                amount = Float.valueOf(amount_str);
            } catch (NumberFormatException e) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("参数错误：amount必须为数字，amount=" + amount_str);
                logger.error("参数错误：amount必须为数字，amount=" + amount_str);
                return retInfo;
            }

            String paymentChannel = map.get("channel");
            if (paymentChannel == null || "".equals(paymentChannel.trim())) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("参数错误：channel，channel=" + paymentChannel);
                logger.error("参数错误：channel，channel=" + paymentChannel);
                return retInfo;
            }

            retInfo = fourLiveService.liveTransfer(
                    tdNtMember.getMemb_id(), amount, Ip.getIpAddr(request), paymentChannel);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}
